import { Page_Prefab, UI_Layer } from "../../constants/ui_def";
import { gg } from "../../frame/gg";
import { he } from "../../frame/he";
import { common } from "../../login/common";


const { ccclass, property } = cc._decorator;

@ccclass
export default class uiBase extends cc.Component {

    uiName = 'page'

    @property({
        type: cc.Integer,
        visible: false
    })
    loadCount = 0

    @property({
        type: cc.Integer,
        visible: false
    })
    openTime = 0
    // 页面打开次数
    @property({
        type: cc.Enum(UI_Layer),
        visible: false
    })
    layer = UI_Layer.Base

    @property({
        type: cc.Node,
        visible: false
    })
    page_node = null
    @property(cc.Node)
    background_node: cc.Node = null    // 黑底节点
    @property(cc.Button)
    close_buttons: cc.Button[] = []   // 关闭按钮列表
    info = null

    /**
   * @description 初始化面板
   * @param {Object} info 
   * @param {String} info.name 必填 面板信息
   * @param {String} info.page   面板使用page目录下其他的预制体。一般默认 同面板信息同名
   * @param {String} info.url    面板使用其他目录预制体。一般默认 同面板信息同名
   * @param {String} info.script 面板使用其他的脚本。一般默认 同面板信息同名
   * @param {String} info.updatePage  面板底部层
   * @param {String} info.cover_opacity 面板底部层颜色
   * @param {String} info.close  面板底部点击可以关闭
   * @param {String} info.alignment 首帧对齐
   * 
   *
   */
    initInfo(info) {
        this.info = info
        this.uiName = info.name     // 面板信息
        this.node.name = info.name  // 节点信息
        this.layer = info.layer
        this.node.setContentSize(gg.uiManager.canvasSize)
        this.checkUibase()
        if (this.checkPrefabs()) {
            this.loadOver()
        }
    }

    // 关闭面板 若对应脚本存在
    closePage() {
        if (!this.node.active) {
            return
        }
        if (gg.uiManager.pageDict[this.info.name + '_' + this.layer] && gg.uiManager.pageDict[this.info.name + '_' + this.layer].closeUI) {
            gg.uiManager.pageDict[this.info.name + '_' + this.layer].closeUI()
        }
        this.node.active = false
    }
    // 其他检测项 比如背景 和 关闭按钮
    checkUibase() {
        this.updateCover()          // 检查遮罩层
        this.updateCloseButton()    // 关闭按钮
    }
    // 加载页面预制体
    checkPrefabs() {
        let checkover = true
        // true为直接打开 false为需要加载
        let pageurl = 'page/'
        if (this.info.url) {
            pageurl = this.info.url
        } else if (this.info.page) {
            pageurl += this.info.page
        } else {
            pageurl += this.uiName
        }
        if (he.Load.get(pageurl, cc.Prefab, null)) {
            this.loadCount += 1
        } else {
            he.Load.loadRes(pageurl, cc.Prefab, this.addLoadCount.bind(this), null)
            checkover = false
        }

        if (Page_Prefab[this.uiName] && Page_Prefab[this.uiName].length) {
            for (let i = 0; i < Page_Prefab[this.uiName].length; i++) {
                let info = {
                    name: '',
                    type: cc.Prefab,
                    bundle: ''
                }
                if (typeof Page_Prefab[this.uiName][i] === 'string') {
                    info.name = Page_Prefab[this.uiName][i]
                    info.type = cc.Prefab
                } else {
                    info = Page_Prefab[this.uiName][i]
                }
                let loadres = he.Load.get(info.name, info.type, null)
                if (loadres) {
                    this.loadCount += 1
                } else {
                    if (checkover) {
                        checkover = false
                        gg.uiManager.switchLoad(true, null)
                    }
                    he.Load.loadRes(info.name, info.type, this.addLoadCount.bind(this), info.bundle)
                }
            }
        }
        return checkover
    }
    // 计数增加
    addLoadCount(err, asset) {
        if (asset) {
            this.loadCount += 1
            if (Page_Prefab[this.uiName]) {
                if (Page_Prefab[this.uiName].length + 1 == this.loadCount) {
                    this.loadOver()
                }
            } else {
                this.loadOver()
            }
        }
    }
    // 加载完成
    loadOver() {
        if (!this.info) {
            return
        }
        // console.log(this.info)
        // gg.eventManager.emit(`${this.info.name}`,{type:'guide'})
        let pageNode = this.node.getChildByName(this.uiName)
        if (!pageNode) {
            let pageurl = 'page/'
            if (this.info.url) {
                pageurl = this.info.url
            } else if (this.info.page) {
                pageurl += this.info.page
            } else {
                pageurl += this.uiName
            }
            pageNode = cc.instantiate(he.Load.get(pageurl, cc.Prefab, null))
            pageNode.name = this.uiName
            pageNode.scale = common.zoom_scale
            this.node.insertChild(pageNode, 2)
        }
        let pageScript
        if (this.uiName != 'loading') {
            if (this.info.script) {
                pageScript = pageNode.getComponent(this.info.script)
            } else {
                pageScript = pageNode.getComponent(this.uiName)
            }
            if (pageScript) {
                gg.uiManager.pageDict[this.info.name + '_' + this.layer] = pageScript
                pageScript.uiBase = this

                if (this.info && this.info.alignment && pageNode.getComponent(cc.Widget)) {
                    let pageWidget = pageNode.getComponent(cc.Widget)
                    pageWidget.updateAlignment()
                }
                if (!this.openTime) {
                    pageScript.initPage(this.info)
                }
                pageScript.updatePage(this.info)
            } else {
                console.warn(this.uiName, "没有绑定脚本 脚本名称：", this.info.script)
            }
            gg.uiManager.switchLoad(false, null)
        }
        if (!this.node) {
            return
        }
        this.node.active = true
        this.page_node = pageNode
        this.openTime += 1
    }
    // 给关闭按钮绑定事件
    updateCloseButton() {
        if (this.info.close) {
            for (let i = 0; i < this.close_buttons.length; i++) {
                this.close_buttons[i].interactable = this.info.close
                let clickEventHandler = new cc.Component.EventHandler();
                clickEventHandler.target = this.node;
                clickEventHandler.component = "uiBase";// 这个是代码文件名
                clickEventHandler.handler = "closePage";
                this.close_buttons[i].clickEvents[0] = clickEventHandler
            }
        } else {
            for (let i = 0; i < this.close_buttons.length; i++) {
                this.close_buttons[i].interactable = false
            }
        }
    }
    // 按钮状态调整
    updateCloseButtonStatus(status) {
        for (let i = 0; i < this.close_buttons.length; i++) {
            this.close_buttons[i].interactable = status
        }
    }
    // 更新覆盖
    updateCover() {
        if (this.info.cover) {
            this.background_node.active = this.info.cover
            if (this.info.cover_opacity >= 0) {
                this.background_node.opacity = this.info.cover_opacity
            } else {
                this.background_node.opacity = 150
            }
        } else {
            this.background_node.active = false
        }
    }
}
